package com.creditcmsplt.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.creditcmsplt.common.CommonPhpService;
import com.creditcmsplt.entity.Token;
import com.creditcmsplt.entity.param.BCreditbiReq;
import com.creditcmsplt.entity.param.BCreditbiReqData;
import com.creditcmsplt.entity.param.BCreditbiReqSystem;
import com.creditcmsplt.entity.param.BCreditbiRsp;
import com.creditcmsplt.entity.param.BCreditbiRspData;
import com.creditcmsplt.service.CreditApiService;
import com.creditcmsplt.service.CreditAuothService;
import com.creditcmsplt.service.CreditSysLogService;
import com.creditcmsplt.util.TextUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("bcredit")
@Api(value = "bcredit", tags = { "6.0,基本信息查询（恒信永利）" },description="bcredit")
public class BasicCreditController {

	private final Logger log = LoggerFactory.getLogger(BasicCreditController.class);
	
	@Autowired
	private CreditAuothService creditAuothService;

	@Autowired
	private CommonPhpService phpService;
	
	@Autowired
	private CreditApiService creditApiService;
	
	@Autowired
	private CreditSysLogService creditSysLogService;
	
	@ApiOperation(value="基本信息查询",notes="基本信息查询包含信息：证件照、持证照、个人基本信息、报告文件")
	@RequestMapping(value = "/getBasicInfo", method = RequestMethod.POST)
	public BCreditbiRsp basicQuery(@RequestBody BCreditbiReq params,HttpServletRequest req) {
		BCreditbiRsp bCreditbiRsp=new BCreditbiRsp();
		BCreditbiReqSystem systemInfo=params.getSystem();
		BCreditbiReqData dataParam=params.getData();
		
		int code=1;
		String msg="成功";
		if(code==1&&(systemInfo==null||TextUtils.isEmpty(systemInfo.getToken()))) {
			code=-1;
			msg="没有授权信息";
		}
		if(code==1&&dataParam==null) {
			code=-1;
			msg="没有必须参数";
		}
		if(code==1&&TextUtils.isEmpty(dataParam.getIdentifyno())) {
			code=-1;
			msg="被查人证件号码为空";
		}
		Map<String, Object> creditAuoth =code==1?this.creditAuothService.checkToken(systemInfo.getToken()):new HashMap<String, Object>();
		if(code==1&&(creditAuoth==null||creditAuoth.get("userid")==null)) {
			code=-1;
			msg="token过期，请重新获取token";
		}//credit_id
		Token token=code==1?creditSysLogService.getToken((Long)creditAuoth.get("credit_id")):null;
		if(code==1&&token==null) {
			code=-1;
			msg="token过期，请重新获取token";
		}
		creditApiService.insert(systemInfo.getToken(), "", "", code, req.getServletPath());//统计接口调用次数
		
		BCreditbiRspData data=code==1?phpService.getUserInfo(token.getApitoken(), dataParam.getIdentifyno()):new BCreditbiRspData();
		String gender=data.getGender();
		if(!TextUtils.isEmpty(gender)&&gender.equals("1")) {
			gender="男";
		}
		if(!TextUtils.isEmpty(gender)&&gender.equals("2")) {
			gender="女";
		}
		code=data.getCode()==null?code:data.getCode();
		msg=data.getMsg()==null?msg:data.getMsg();
		data.setCode(code);
		data.setMsg(msg);
		bCreditbiRsp.setData(data);
		return bCreditbiRsp;
	}
}
